package com.simon.onekey;

import androidx.appcompat.app.AppCompatActivity;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    private ComponentName mAdminName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdminName = new ComponentName(this, AdminManageReceiver.class);
        //获取设备管理器
        DevicePolicyManager mDPM = (DevicePolicyManager) this.getSystemService(Context.DEVICE_POLICY_SERVICE);
        //如果还没有激活设备管理器，则直接进入到设备管理器界面
        if (!mDPM.isAdminActive(mAdminName)) {
            showAdminManage();
        }
        //如果已经有了设备管理器的权限，则锁定屏幕
        if (mDPM.isAdminActive(mAdminName)) {
            mDPM.lockNow();
        }
        finish();
    }

    private void showAdminManage() {
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, R.string.app_name);
        startActivityForResult(intent, -1);
    }
}

